package com.yatu.codegen.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.yatu.codegen.util.PropertiesUtil;

public class DBConn {
	// 定义一个连接对象
	private Connection conn = null;
	private static String driver;
	// 定义连接数据库的url资源
	private static String url;
	// 定义连接数据库的用户名和密码
	private static String userName;
	private static String passWord;

	static {
		driver = PropertiesUtil.load("jdbc.driver", "com.mysql.jdbc.Driver");
		url = PropertiesUtil.load("jdbc.url", "jdbc:mysql://localhost:3306/db_gym?characterEncoding=utf8");
		userName = PropertiesUtil.load("jdbc.userName", "root");
		passWord = PropertiesUtil.load("jdbc.passWord", "root");
	}

	// 加载数据库连接驱动
	public Connection getConnection() {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, userName, passWord);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	// 关闭数据库
	public void closeALL(Connection conn, Statement st, ResultSet rs, PreparedStatement pst) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (st != null) {
				st.close();
			}
			if (conn != null) {
				conn.close();
			}
			if (pst != null)
				pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
